import { toLower } from 'lodash';
import warning from 'warning';
import { PluginType } from './interface';

export class PluginRegistry {
  public renderTableCellRegister = (type: string, callback: Function) => {};

  public tableSelectedRowsRegister = (type: string, callback: Function) => {};

  public convertSearchParamsRegister = (type: string, callback: Function) => {};

  public convertDataSourceRegister = (type: string, callback: Function) => {};

  public convertDisplayValueRegister = (type: string, callback: Function) => {};

  public tableCheckboxRowPropsRegister = (type: string, callback: Function) => {};

  public convertSubmitParamsValueRegister = (type: string, callback: Function) => {};

  public convertTableSelectedRowsRegister = (type: string, callback: Function) => {};

  public convertDetailValuesRegister = (type: string, callback: Function) => {};

  public registerType = (name: PluginType) => {
    // @ts-ignore
    const types = (this[`${name}Types`] = Object.create(null));

    // 注册插件
    // @ts-ignore
    this[`${name}Register`] = (type: string, callback) => {
      // 大小写无关
      const finalType = toLower(type);

      warning(!(finalType in types), `[InputModal 插件注册]  type ${type} 已存在, 默认覆盖`);

      types[finalType] = callback;
    };

    // 执行插件
    // @ts-ignore
    this[name] = (type: string, config: any) => {
      const finalType = toLower(type);
      warning(finalType in types, `[InputModal 插件执行]  type ${type} 无法找到`);
      return types[finalType]?.(config);
    };
  };
}

const plugin = new PluginRegistry();

plugin.registerType('renderTableCell');
plugin.registerType('tableCheckboxRowProps');

plugin.registerType('convertSearchParams');
plugin.registerType('convertDataSource');
plugin.registerType('convertDisplayValue');
plugin.registerType('convertDetailValues');
plugin.registerType('convertSubmitParams');
plugin.registerType('convertTableSelectedRows');

export default plugin;
